﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using DDD;
using FloatComparer = UnitTest.TestHelper.FloatComparer;

namespace UnitTest {
    [TestClass]
    public class Test_OfflineRendering_R32i_ {

        #region Field
        GameWindow window;
        World wld;
        Graphics3D g3d;
        FrameBuffer fbuf;
        #endregion

        #region Setup
        [TestInitialize ()]
        public void MyClassInitialize () {
            window = new GameWindow ();
            wld = CreateWorld ();
            fbuf = CreateFrameBuffer ();

            g3d = Graphics3D.GetInstance ();
            g3d.Reset ();

            g3d.RenderPass += delegate {
                g3d.SetFrameBuffer (fbuf);
                g3d.SetWorld (wld);
                g3d.SetViewort (0, 0, 2, 2);
            };
        }

        [TestCleanup ()]
        public void MyClassCleanup () {
            window.Dispose ();
            window = null;
            wld = null;
        }

        private World CreateWorld () {
            var cam = new Camera ();
            cam.SetPerspective (45, 1, 0.1f, 10f);

            var camNode = new Node ();
            camNode.Attach (cam);
            camNode.SetTranslation (0, 0, 1);

            var pos = new float[12] { -0.5f, -0.5f, 0,
                                       0.5f, -0.5f, 0,
                                      -0.5f, 0.5f, 0,
                                       0.5f, 0.5f, 0};
            var col = new int[4] { -1, 0, 1, 2 };
            var posArray = new VertexArray<float> (4, 3);
            var colArray = new VertexArray<int> (4, 1);
            posArray.Set (0, 4, pos);
            colArray.Set (0, 4, col);

            var vbuf = new VertexBuffer ();
            vbuf.AddVertexArray ("v_Position", posArray);
            vbuf.AddVertexArray ("v_Color", colArray);

            var ibuf = new IndexBuffer<int> (PrimitiveType.Point, 4);
            var indices = new int[] { 0, 1, 2, 3 };
            ibuf.Set (0, 4, indices);

            var vert = @"#version 400
                         in  vec3 v_Position;
                         in  int v_Color;
                         flat out int f_Color;
                               
                         void main () {
                             gl_Position = vec4(v_Position, 1);
                             f_Color = v_Color;
                         }
                        ";
            var frag = @"#version 400
                         flat in  int f_Color;
                         out int frag_Color;
                               
                         void main () {
                             frag_Color = f_Color;
                         }
                        ";

            var vs = new VertexShader (new MemoryStream (Encoding.UTF8.GetBytes (vert)));
            var fs = new FragmentShader (new MemoryStream (Encoding.UTF8.GetBytes (frag)));
            var sp = new ShaderProgram (vs, fs);

            var app = new Appearance ();
            app.SetShaderProgram (sp);

            var msh = new Mesh ();
            msh.SetVertexBuffer (vbuf);
            msh.SetIndexBuffer (ibuf);
            msh.SetAppearance (app);

            var mshNode = new Node ();
            mshNode.Attach (msh);

            var wld = new World ();
            wld.AddChild (camNode);
            wld.AddChild (mshNode);
            wld.SetActiveCamera (camNode);

            return wld;
        }

        private static FrameBuffer CreateFrameBuffer () {
            var fbuf = new FrameBuffer (1, 0, 0);
            var color = new Image2D<int> (DDD.PixelFormat.R32i, 2, 2);
            fbuf.SetColorBuffer (0, color);
            return fbuf;
        }
        #endregion



        #region TestMethod
        [TestMethod]
        public void TestMethod1 () {

            g3d.Render ();
            window.SwapBuffers ();

            GL.ClampColor (ClampColorTarget.ClampReadColor, ClampColorMode.FixedOnly);

            var expected = new int[4] { -1, 0, 1, 2 };
            var pixels = new int[4];
            var color = fbuf.GetColorBuffer (0) as Image2D<int>;
            color.Capture ();
            color.Get (0, 0, 2, 2, pixels);

            CollectionAssert.AreEqual (expected, pixels);
        }
        #endregion



    }
}
